package com.xusheng.cli.service.impl;

import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.MyServiceImpl;
import com.xusheng.cli.entity.dictionaryModule.Category;
import com.xusheng.cli.entity.favoritesModule.Favorites;
import com.xusheng.cli.entity.videoModule.Video;
import com.xusheng.cli.mapper.FavoritesMapper;
import com.xusheng.cli.req.FavoritesQueryReq;
import com.xusheng.cli.service.FavoritesService;
import com.xusheng.cli.tools.CurrentPlayerUserUtil;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;

/**
 * 收藏夹(Favorites)表服务实现类
 *
 * @author lyh
 * @since 2023-04-27 17:23:53
 */
@Service("favoritesService")
public class FavoritesServiceImpl extends MyServiceImpl<FavoritesMapper, Favorites> implements FavoritesService {

    @Override
    public MyPage<Video> favoritesPage(FavoritesQueryReq req, boolean flag) {
        JoinLambdaWrapper<Favorites> wrapper = new JoinLambdaWrapper<>(Favorites.class);
        wrapper.notDefaultSelectAll()
                .eq(Favorites::getUserId, req.getUserId())
                .gt(flag, Favorites::getCreateTime, LocalDateTime.now().with(LocalTime.MIN))
                .lt(flag, Favorites::getCreateTime, LocalDateTime.now().with(LocalTime.MAX))
                .orderByDesc(Favorites::getCreateTime)
                .innerJoin(Video.class, Video::getId, Favorites::getVideoId)
                .selectAll()
                .eq(req.getCategoryId() != null, Video::getCategoryId, req.getCategoryId())
                .end();
        return baseMapper.joinSelectPage(req.getPage(), wrapper, Video.class);
    }

    @Override
    public List<Category> categoryByCollect() {
        // 获取当前登录人信息
        Long userId = CurrentPlayerUserUtil.getUserId();
        return baseMapper.categoryByCollect(userId);
    }

    @Override
    public List<Video> listByCategory(Long id) {
        // 获取当前登录人信息
        Long userId = CurrentPlayerUserUtil.getUserId();
        return baseMapper.listByCategory(id, userId);
    }
}

